﻿#region Namespace References

#endregion

#region Namespace References

using System;
using System.Windows.Forms;
using DynConDevCorp.OpenArch.SmartData.Interfaces;

#endregion

namespace DynConDevCorp.OpenArch.SmartData.WinForms
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SmartEntityCollectionControl : SmartEntityControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SmartEntityCollectionControl"/> class.
        /// </summary>
        public SmartEntityCollectionControl()
        {
            InitializeComponent();
            EntityBindingManager.BoundEntityChanged += EntityBindingManager_BoundEntityChanged;
        }

        #endregion

        #region Protected Instance Properties

        /// <summary>
        /// Gets or sets the type of the item control.
        /// </summary>
        /// <value>The type of the item control.</value>
        protected Type ItemControlType { get; set; }

        #endregion

        #region Private Instance Methods

        private void EntityBindingManager_BoundEntityChanged(object sender, EventArgs e)
        {
            m_BoundCollection = EntityBindingManager.BoundEntity as ISmartEntityCollection;
            PopulateControl();
        }

        private void PopulateControl()
        {
            RemoveItemControls();
            int offset = 0;
            foreach (ISmartEntity item in m_BoundCollection)
            {
                SmartEntityControl itemControl = (SmartEntityControl) Activator.CreateInstance(ItemControlType);
                itemControl.Top = offset;
                itemControl.Width = pnlItems.Width;
                itemControl.BoundEntity = item;
                itemControl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
                pnlItems.Controls.Add(itemControl);
                offset += itemControl.Height;
            }
            vsbItems.Maximum = offset;
            vsbItems.LargeChange = vsbItems.Height;
        }

        private void RemoveItemControls()
        {
            pnlItems.Controls.Clear();
        }

        private void vsbItems_Scroll(object sender, ScrollEventArgs e)
        {
        }

        #endregion

        #region Fields and Constants

        private ISmartEntityCollection m_BoundCollection;

        #endregion
    }
}